﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMS_GYM.Data;
using CMS_GYM.Events;

namespace CMS_GYM.Interface
{
    public partial class frmDatLichSanCauMoi : frmTemplete1
    {
        clsLichChiTietBUS LichChiTietBUS = new clsLichChiTietBUS();
        clsSanCauBUS SanCauBUS = new clsSanCauBUS();
        clsKhachHangBUS KhachHangBUS = new clsKhachHangBUS();
        clsLoaiKhachHangBUS LoaiKhachHangBUS = new clsLoaiKhachHangBUS();
        int SoLuongSanCau = 0;
        //DataTable dtSanCau = new DataTable();
        #region Initial
        public frmDatLichSanCauMoi()
        {
            InitializeComponent();
            lblTitle.Text = "ĐẶT LỊCH SÂN CẦU LÔNG";
            uct_Reset.Reset.Click += new EventHandler(btn_Reset_Click);
            uct_Save.Save.Click += new EventHandler(btn_Save_Click);
            uct_Refresh.Refresh.Click += new EventHandler(btn_Refresh_Click);
            dscSanCau01.AppointmentCreate += calendar_AppointmentAdd;
            dscSanCau01.AppointmentMove += calendar_AppointmentMove;
            dscSanCau01.AppointmentEdit += calendar_AppointmentEdit;
            dscSanCau02.AppointmentCreate += calendar_AppointmentAdd;
            dscSanCau02.AppointmentMove += calendar_AppointmentMove;
            dscSanCau02.AppointmentEdit += calendar_AppointmentEdit;
            dscSanCau03.AppointmentCreate += calendar_AppointmentAdd;
            dscSanCau03.AppointmentMove += calendar_AppointmentMove;
            dscSanCau03.AppointmentEdit += calendar_AppointmentEdit;
        }
        #endregion

        #region Functions

        private void SetPermission()
        {
            uct_Reset.Enabled = clsUser.LayThongTinPhanQuyen("LichSanCau").ChucNang.Them;
            uct_Save.Enabled = clsUser.LayThongTinPhanQuyen("LichSanCau").ChucNang.Sua;
            btnLuuLichSan.Enabled = clsUser.LayThongTinPhanQuyen("LichSanCau").ChucNang.Xoa;
            //uct_Delete.Enabled = clsUser.LayThongTinPhanQuyen("LichSanCau").ChucNang.Xoa;
        }

        #region LoadData()
        private void LoadData()
        {
            DataTable dtSanCau = SanCauBUS.LayBang();
            SoLuongSanCau = dtSanCau.Rows.Count;
            DateTime cur = DateTime.Now;
            DateTime weekstart = new DateTime(cur.Year, cur.Month, cur.Day, 0, 0, 0);
            AppointmentList apptsSan1 = new AppointmentList();
            AppointmentList apptsSan2 = new AppointmentList();
            AppointmentList apptsSan3 = new AppointmentList();
            if (dtSanCau.Rows.Count == 2)
            {
                apptsSan1 = TaiDuLieuLichCauLong(int.Parse(dtSanCau.Rows[0]["SanCauId"].ToString()), weekstart);
                apptsSan2 = TaiDuLieuLichCauLong(int.Parse(dtSanCau.Rows[1]["SanCauId"].ToString()), weekstart);
            }
            else if (dtSanCau.Rows.Count == 3)
            {
                apptsSan1 = TaiDuLieuLichCauLong(int.Parse(dtSanCau.Rows[0]["SanCauId"].ToString()), weekstart);
                apptsSan2 = TaiDuLieuLichCauLong(int.Parse(dtSanCau.Rows[1]["SanCauId"].ToString()), weekstart);
                apptsSan3 = TaiDuLieuLichCauLong(int.Parse(dtSanCau.Rows[2]["SanCauId"].ToString()), weekstart);
            }
            dscSanCau01.Date = weekstart;
            dscSanCau01.Appointments = apptsSan1;
            dscSanCau02.Date = weekstart;
            dscSanCau02.Appointments = apptsSan2;
            dscSanCau03.Date = weekstart;
            dscSanCau03.Appointments = apptsSan3;
        }
        #endregion

        #region AppointmentList TaiDuLieuLichCauLong(int SanCauId, DateTime date)
        private AppointmentList TaiDuLieuLichCauLong(int SanCauId, DateTime date)
        {
            try
            {
                var appts = new AppointmentList();
                var rand = new Random();
                DataTable table = LichChiTietBUS.LayBangTheoSanCauId(SanCauId);
                if (table.Rows.Count == 0) return appts;
                switch (date.DayOfWeek)
                {
                    #region Monday
                    case DayOfWeek.Monday:
                        foreach (DataRow dr in table.Rows)
                        {
                            ExtendedAppointment appt = new ExtendedAppointment();
                            string[] GioBatDau = dr["GioBatDau"].ToString().Split(':');
                            string[] GioKetThuc = dr["GioKetThuc"].ToString().Split(':');
                            switch (dr["TenNgayTrongTuan"].ToString().ToUpper())
                            {
                                case "THỨ HAI":
                                    appt.DateStart = date.AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BA":
                                    appt.DateStart = date.AddDays(1).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(1).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ TƯ":
                                    appt.DateStart = date.AddDays(2).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(2).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ NĂM":
                                    appt.DateStart = date.AddDays(3).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(3).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ SÁU":
                                    appt.DateStart = date.AddDays(4).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(4).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BẢY":
                                    appt.DateStart = date.AddDays(5).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(5).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "CHỦ NHẬT":
                                    appt.DateStart = date.AddDays(6).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(6).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                            }
                            appt.Subject = dr["HoTen"].ToString() + (dr["MoTa"] == null? string.Empty : dr["MoTa"].ToString());
                            appt.LichChiTietId = int.Parse(dr["LichChiTietId"].ToString());
                            appt.LichId = int.Parse(dr["LichId"].ToString());
                            appt.NgayTrongTuanId = int.Parse(dr["NgayTrongTuanId"].ToString());
                            appts.Add(appt);
                        }
                        break;
                    #endregion

                    #region Tuesday
                    case DayOfWeek.Tuesday:
                        foreach (DataRow dr in table.Rows)
                        {
                            ExtendedAppointment appt = new ExtendedAppointment();
                            string[] GioBatDau = dr["GioBatDau"].ToString().Split(':');
                            string[] GioKetThuc = dr["GioKetThuc"].ToString().Split(':');
                            switch (dr["TenNgayTrongTuan"].ToString().ToUpper())
                            {
                                case "THỨ HAI":
                                    appt.DateStart = date.AddDays(-1).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-1).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BA":
                                    appt.DateStart = date.AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ TƯ":
                                    appt.DateStart = date.AddDays(1).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(1).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ NĂM":
                                    appt.DateStart = date.AddDays(2).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(2).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ SÁU":
                                    appt.DateStart = date.AddDays(3).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(3).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BẢY":
                                    appt.DateStart = date.AddDays(4).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(4).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "CHỦ NHẬT":
                                    appt.DateStart = date.AddDays(5).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(5).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                            }
                            appt.Subject = dr["HoTen"].ToString() + (dr["MoTa"] == null ? string.Empty : dr["MoTa"].ToString());
                            appt.LichChiTietId = int.Parse(dr["LichChiTietId"].ToString());
                            appt.LichId = int.Parse(dr["LichId"].ToString());
                            appt.NgayTrongTuanId = int.Parse(dr["NgayTrongTuanId"].ToString());
                            appts.Add(appt);
                        }
                        break;
                    #endregion

                    #region Wednesday
                    case DayOfWeek.Wednesday:
                        foreach (DataRow dr in table.Rows)
                        {
                            ExtendedAppointment appt = new ExtendedAppointment();
                            string[] GioBatDau = dr["GioBatDau"].ToString().Split(':');
                            string[] GioKetThuc = dr["GioKetThuc"].ToString().Split(':');
                            switch (dr["TenNgayTrongTuan"].ToString().ToUpper())
                            {
                                case "THỨ HAI":
                                    appt.DateStart = date.AddDays(-2).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-2).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BA":
                                    appt.DateStart = date.AddDays(-1).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-1).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ TƯ":
                                    appt.DateStart = date.AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ NĂM":
                                    appt.DateStart = date.AddDays(1).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(1).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ SÁU":
                                    appt.DateStart = date.AddDays(2).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(2).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BẢY":
                                    appt.DateStart = date.AddDays(3).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(3).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "CHỦ NHẬT":
                                    appt.DateStart = date.AddDays(4).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(4).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                            }
                            appt.Subject = dr["HoTen"].ToString() + (dr["MoTa"] == null ? string.Empty : dr["MoTa"].ToString());
                            appt.LichChiTietId = int.Parse(dr["LichChiTietId"].ToString());
                            appt.LichId = int.Parse(dr["LichId"].ToString());
                            appt.NgayTrongTuanId = int.Parse(dr["NgayTrongTuanId"].ToString());
                            appts.Add(appt);
                        }
                        break;
                    #endregion Wednesday

                    #region Thursday
                    case DayOfWeek.Thursday:
                        foreach (DataRow dr in table.Rows)
                        {
                            ExtendedAppointment appt = new ExtendedAppointment();
                            string[] GioBatDau = dr["GioBatDau"].ToString().Split(':');
                            string[] GioKetThuc = dr["GioKetThuc"].ToString().Split(':');
                            switch (dr["TenNgayTrongTuan"].ToString().ToUpper())
                            {
                                case "THỨ HAI":
                                    appt.DateStart = date.AddDays(-3).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-3).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BA":
                                    appt.DateStart = date.AddDays(-2).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-2).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ TƯ":
                                    appt.DateStart = date.AddDays(-1).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-1).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ NĂM":
                                    appt.DateStart = date.AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ SÁU":
                                    appt.DateStart = date.AddDays(1).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(1).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BẢY":
                                    appt.DateStart = date.AddDays(2).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(2).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "CHỦ NHẬT":
                                    appt.DateStart = date.AddDays(3).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(3).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                            }
                            appt.Subject = dr["HoTen"].ToString() + (dr["MoTa"] == null ? string.Empty : dr["MoTa"].ToString());
                            appt.LichChiTietId = int.Parse(dr["LichChiTietId"].ToString());
                            appt.LichId = int.Parse(dr["LichId"].ToString());
                            appt.NgayTrongTuanId = int.Parse(dr["NgayTrongTuanId"].ToString());
                            appts.Add(appt);
                        }
                        break;
                    #endregion

                    #region Friday
                    case DayOfWeek.Friday:
                        foreach (DataRow dr in table.Rows)
                        {
                            ExtendedAppointment appt = new ExtendedAppointment();
                            string[] GioBatDau = dr["GioBatDau"].ToString().Split(':');
                            string[] GioKetThuc = dr["GioKetThuc"].ToString().Split(':');
                            switch (dr["TenNgayTrongTuan"].ToString().ToUpper())
                            {
                                case "THỨ HAI":
                                    appt.DateStart = date.AddDays(-4).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-4).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BA":
                                    appt.DateStart = date.AddDays(-3).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-3).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ TƯ":
                                    appt.DateStart = date.AddDays(-2).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-2).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ NĂM":
                                    appt.DateStart = date.AddDays(-1).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-1).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ SÁU":
                                    appt.DateStart = date.AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BẢY":
                                    appt.DateStart = date.AddDays(1).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(1).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "CHỦ NHẬT":
                                    appt.DateStart = date.AddDays(2).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(2).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                            }
                            appt.Subject = dr["HoTen"].ToString() + (dr["MoTa"] == null ? string.Empty : dr["MoTa"].ToString());
                            appt.LichChiTietId = int.Parse(dr["LichChiTietId"].ToString());
                            appt.LichId = int.Parse(dr["LichId"].ToString());
                            appt.NgayTrongTuanId = int.Parse(dr["NgayTrongTuanId"].ToString());
                            appts.Add(appt);
                        }
                        break;
                    #endregion

                    #region Saturday
                    case DayOfWeek.Saturday:
                        foreach (DataRow dr in table.Rows)
                        {
                            ExtendedAppointment appt = new ExtendedAppointment();
                            string[] GioBatDau = dr["GioBatDau"].ToString().Split(':');
                            string[] GioKetThuc = dr["GioKetThuc"].ToString().Split(':');
                            switch (dr["TenNgayTrongTuan"].ToString().ToUpper())
                            {
                                case "THỨ HAI":
                                    appt.DateStart = date.AddDays(-5).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-5).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BA":
                                    appt.DateStart = date.AddDays(-4).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-4).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ TƯ":
                                    appt.DateStart = date.AddDays(-3).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-3).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ NĂM":
                                    appt.DateStart = date.AddDays(-2).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-2).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ SÁU":
                                    appt.DateStart = date.AddDays(-1).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-1).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BẢY":
                                    appt.DateStart = date.AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "CHỦ NHẬT":
                                    appt.DateStart = date.AddDays(1).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(1).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                            }
                            appt.Subject = dr["HoTen"].ToString() + (dr["MoTa"] == null ? string.Empty : dr["MoTa"].ToString());
                            appt.LichChiTietId = int.Parse(dr["LichChiTietId"].ToString());
                            appt.LichId = int.Parse(dr["LichId"].ToString());
                            appt.NgayTrongTuanId = int.Parse(dr["NgayTrongTuanId"].ToString());
                            appts.Add(appt);
                        }
                        break;
                    #endregion

                    #region Sunday
                    case DayOfWeek.Sunday:
                        foreach (DataRow dr in table.Rows)
                        {
                            ExtendedAppointment appt = new ExtendedAppointment();
                            string[] GioBatDau = dr["GioBatDau"].ToString().Split(':');
                            string[] GioKetThuc = dr["GioKetThuc"].ToString().Split(':');
                            switch (dr["TenNgayTrongTuan"].ToString().ToUpper())
                            {
                                case "THỨ HAI":
                                    appt.DateStart = date.AddDays(-6).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-6).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BA":
                                    appt.DateStart = date.AddDays(-5).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-5).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ TƯ":
                                    appt.DateStart = date.AddDays(-4).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-4).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ NĂM":
                                    appt.DateStart = date.AddDays(-3).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-3).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ SÁU":
                                    appt.DateStart = date.AddDays(-2).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-2).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                                case "THỨ BẢY":
                                    appt.DateStart = date.AddDays(-1).AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddDays(-1).AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));

                                    break;
                                case "CHỦ NHẬT":
                                    appt.DateStart = date.AddHours(double.Parse(GioBatDau[0])).AddMinutes(double.Parse(GioBatDau[1])).AddSeconds(double.Parse(GioBatDau[2]));
                                    appt.DateEnd = date.AddHours(double.Parse(GioKetThuc[0])).AddMinutes(double.Parse(GioKetThuc[1])).AddSeconds(double.Parse(GioKetThuc[2]));
                                    break;
                            }
                            appt.Subject = dr["HoTen"].ToString() + (dr["MoTa"] == null ? string.Empty : dr["MoTa"].ToString());
                            appt.LichChiTietId = int.Parse(dr["LichChiTietId"].ToString());
                            appt.LichId = int.Parse(dr["LichId"].ToString());
                            appt.NgayTrongTuanId = int.Parse(dr["NgayTrongTuanId"].ToString());
                            appts.Add(appt);
                        }
                        break;
                    #endregion
                }
                appts.SortAppointments();
                return appts;
            }
            catch { return new AppointmentList(); }
        }
        #endregion

        #region CountNumberOfWeekDayInDateRange(DayOfWeek day, DateTime start, DateTime end)
        public int CountNumberOfWeekDayInDateRange(DayOfWeek day, DateTime start, DateTime end)
        {
            TimeSpan ts = end - start;                       // Total duration
            int count = (int)Math.Floor(ts.TotalDays / 7);   // Number of whole weeks
            int remainder = (int)(ts.TotalDays % 7);         // Number of remaining days
            int sinceLastDay = (int)(end.DayOfWeek - day);   // Number of days since last [day]
            if (sinceLastDay < 0) sinceLastDay += 7;         // Adjust for negative days since last [day]
            // If the days in excess of an even week are greater than or equal to the number days since the last [day], then count this one, too.
            if (remainder >= sinceLastDay) count++;
            return count;
        }
        #endregion
        #endregion

        #region Events
        #region OnLoad(EventArgs e)
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                base.OnLoad(e);
                DoubleBuffered = true;
                LoadData();
            }
            catch { }
        }
        #endregion

        #region btn_Reset_Click(object sender, EventArgs e)
        private void btn_Reset_Click(object sender, EventArgs e)
        {
            try
            {
                txtMaKhachHang.Text = string.Empty;
                txtHoTen.Text = string.Empty;
                txtDiaChi.Text = string.Empty;
                txtSoDT.Text = string.Empty;
                string MaKhachHangHienHanh = string.Empty;
                MaKhachHangHienHanh = KhachHangBUS.LayMaKhachHangCauLongMoi();
                int MaKhachHangMoi = 0;
                if (MaKhachHangHienHanh == null)
                    MaKhachHangMoi = 1;
                else
                    MaKhachHangMoi = int.Parse(MaKhachHangHienHanh.Substring(2)) + 1;
                if (MaKhachHangMoi<10)
                    txtMaKhachHang.Text = "CL0" + MaKhachHangMoi.ToString();
                else
                    txtMaKhachHang.Text = "CL" + MaKhachHangMoi.ToString();
                txtHoTen.Focus();
            }
            catch { }
        }
        #endregion

        #region btn_Save_Click(object sender, EventArgs e)
        private void btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Xác nhận thêm mới dữ liệu?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Cancel) return;

                DataTable dtLoaiKhachHang = LoaiKhachHangBUS.LayBang();
                DataView dv = new DataView(dtLoaiKhachHang);
                dv.RowFilter = "TenLoaiKhachHang = 'Cầu lông'";

                clsKhachHangDTO KhachHangDTO = new clsKhachHangDTO();
                KhachHangDTO.MaKhachHang = txtMaKhachHang.Text.Trim();
                KhachHangDTO.HoTen = txtHoTen.Text.Trim();
                KhachHangDTO.SoDienThoai = txtSoDT.Text.Trim();
                KhachHangDTO.DiaChi = txtDiaChi.Text.Trim();
                KhachHangDTO.LoaiKhachHangId = int.Parse(dv.ToTable().Rows[0]["LoaiKhachHangId"].ToString());
                int kq = KhachHangBUS.ThemKhachHangCauLong(KhachHangDTO);
                if (kq == -1)
                {
                    MessageBox.Show("Lỗi thêm mới dữ liệu.", "Thông báo");
                }
                else
                {
                    MessageBox.Show("Thêm mới dữ liệu thành công.", "Thông báo");
                }
                txtMaKhachHang.ResetText();
                txtHoTen.ResetText();
                txtSoDT.ResetText();
                txtDiaChi.ResetText();
            }
            catch { MessageBox.Show("Lỗi thêm mới dữ liệu.", "Thông báo"); }
        }
        #endregion

        #region btn_Refresh_Click(object sender, EventArgs e)
        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            OnLoad(e);
        }
        #endregion

        #region calendar_AppointmentEdit(object sender, AppointmentEditEventArgs e)
        private void calendar_AppointmentEdit(object sender, AppointmentEditEventArgs e)
        {
            try
            {
                // TEST THEM NGAY 9/12/2013

                // xu ly man hinh khach hang vang lai
                #region xu ly man hinh khach hang vang lai
                if (e.Appointment.Subject.Contains("KH vãng lai")) 
                {
                    using (frmSuaLichKhachHangVangLai dialog = new frmSuaLichKhachHangVangLai())
                    {
                        if (e.Appointment.Subject == "KH vãng lai (đã thu)")
                        {
                            dialog.rbtKhachHangVangLaiDaThu.Visible = true;
                            dialog.btnSave.Enabled = false;
                            dialog.btnTinhTien.Enabled = false;
                        }
                        else if (e.Appointment.Subject == "KH vãng lai (chưa thu)")
                        {
                            dialog.rbtKhachHangVangLaiChuaThu.Visible = true;
                        }
                        dialog.AppointmentDateStart = e.Appointment.DateStart;
                        dialog.AppointmentDateEnd = e.Appointment.DateEnd;
                        dialog.AppointmentTitle = e.Appointment.Subject;
                        dialog.LichId = e.Appointment.LichId;
                        dialog.LichChiTietId = e.Appointment.LichChiTietId;
                        dialog.NgayTrongTuanId = e.Appointment.NgayTrongTuanId;
                        
                        DialogResult result = dialog.ShowDialog();
                        if (result == DialogResult.OK)// Cap nhat lich san cau long
                        {
                            clsLichChiTietDTO lichChiTietDTO = new clsLichChiTietDTO();
                            lichChiTietDTO.LichChiTietId = dialog.LichChiTietId;
                            lichChiTietDTO.LichId = dialog.LichId;
                            lichChiTietDTO.NgayTrongTuanId = dialog.NgayTrongTuanId;
                            lichChiTietDTO.GioBatDau = dialog.AppointmentDateStart.ToShortTimeString();
                            lichChiTietDTO.GioKetThuc = dialog.AppointmentDateEnd.ToShortTimeString();
                            lichChiTietDTO.MoTa = dialog.MoTa;
                            clsLichChiTietBUS LichChiTietBUS = new clsLichChiTietBUS();
                            int kq = LichChiTietBUS.Sua(lichChiTietDTO);
                            if (kq == -1)
                            {
                                MessageBox.Show("Lỗi cập nhật lịch chi tiết sân cầu lông", "Thông báo");
                                return;
                            }
                            MessageBox.Show("Cập nhật lịch chi tiết sân cầu thành công", "Thông báo");
                            OnLoad(e);
                            return;
                        }
                        // Xoa lich tren giao dien
                        else if (result == DialogResult.Abort)
                        {
                            int IDLichChiTiet = dialog.LichChiTietId;
                            int kq = LichChiTietBUS.Xoa(IDLichChiTiet);
                            if (kq == -1)
                            {
                                MessageBox.Show("Lỗi xóa lịch chi tiết sân cầu lông", "Thông báo");
                                return;
                            }
                            MessageBox.Show("Xóa lịch chi tiết sân cầu thành công", "Thông báo");
                            OnLoad(e);
                            return;
                        }
                        // thuc hien thao tac tinh tien
                        else if (result == DialogResult.Yes) 
                        {
                            clsCongTyBUS CongTyBus = new clsCongTyBUS();
                            Double GiaCauLongVangLaiTruoc15h = CongTyBus.GiaCauLongVangLaiTruoc15h();
                            Double GiaCauLongVangLaiSau15h = CongTyBus.GiaCauLongVangLaiSau15h();
                            Double GiaCauLongVangLaiCuoiTuan = CongTyBus.GiaCauLongVangLaiCuoiTuan();
                            
                            e.Appointment.DateStart = dialog.AppointmentDateStart;
                            e.Appointment.DateEnd = dialog.AppointmentDateEnd;
                            e.Appointment.Subject = dialog.AppointmentTitle;
                            e.Appointment.LichChiTietId = dialog.LichChiTietId;
                            e.Appointment.LichId = dialog.LichId;
                            e.Appointment.NgayTrongTuanId = dialog.NgayTrongTuanId;
                            
                            TimeSpan ts = e.Appointment.DateEnd.Subtract(e.Appointment.DateStart);
                            double TongTien = 0;
                            //double Gio = ts.TotalMinutes / 60;
                            if (e.Appointment.NgayTrongTuanId > 6) // choi ngay T7, CN
                            {
                                TongTien = (ts.TotalMinutes / 60) * GiaCauLongVangLaiCuoiTuan;
                            }
                            else if (e.Appointment.DateEnd.Hour <= 15) // choi truoc 15g trong tuan
                            {
                                TongTien = (ts.TotalMinutes / 60) * GiaCauLongVangLaiTruoc15h;
                            }
                            else if (e.Appointment.DateStart.Hour >= 15) // choi sau 15g trong tuan
                            {
                                TongTien = (ts.TotalMinutes / 60) * GiaCauLongVangLaiSau15h;
                            }
                            clsKhachHangBUS KhachHangBus = new clsKhachHangBUS();
                            DataTable dtKhachHangCauLong = KhachHangBus.LayBangKhachHangCauLong();
                            DataView dv = new DataView(dtKhachHangCauLong);
                            dv.RowFilter = "MaKhachHang = 'CLVL'";
                            DataTable dtTemp = dv.ToTable();
                            int IDKhachHangVangLai = int.Parse(dtTemp.Rows[0]["KhachHangId"].ToString());
                            string MaKhachHangVangLai = dtTemp.Rows[0]["MaKhachHang"].ToString();
                            frmXuatPhieuThu fXuatPhieuThu = new frmXuatPhieuThu(IDKhachHangVangLai, MaKhachHangVangLai, "KH Vãng lai cầu lông"
                                , e.Appointment.DateStart, e.Appointment.DateEnd, TongTien);
                            fXuatPhieuThu.ShowDialog();
                            if (fXuatPhieuThu.isThuTienVangLaiCauLong == true)
                            {
                                // Cap nhat lai du lieu chuyen tu KH CHUA THU sang KH DA THU
                                clsLichChiTietDTO lichChiTietDTO = new clsLichChiTietDTO();
                                lichChiTietDTO.LichChiTietId = dialog.LichChiTietId;
                                lichChiTietDTO.LichId = dialog.LichId;
                                lichChiTietDTO.NgayTrongTuanId = dialog.NgayTrongTuanId;
                                lichChiTietDTO.GioBatDau = dialog.AppointmentDateStart.ToShortTimeString();
                                lichChiTietDTO.GioKetThuc = dialog.AppointmentDateEnd.ToShortTimeString();
                                lichChiTietDTO.MoTa = dialog.MoTa;

                                clsLichChiTietBUS LCTBUS = new clsLichChiTietBUS();
                                int kq = LCTBUS.Sua(lichChiTietDTO);
                                if (kq == -1)
                                {
                                    MessageBox.Show("Lỗi cập nhật lịch chi tiết sân cầu lông", "Thông báo");
                                    return;
                                }
                                //MessageBox.Show("Cập nhật lịch chi tiết sân cầu thành công", "Thông báo");
                                OnLoad(e);
                            }
                        }
                    }
                    return;
                }
                #endregion

                // xu ly man hinh khach hang thong thuong
                #region xu ly man hinh khach hang thong thuong
                using (frmSuaLichSanCauLong dialog = new frmSuaLichSanCauLong())
                {

                    dialog.AppointmentDateStart = e.Appointment.DateStart;
                    dialog.AppointmentDateEnd = e.Appointment.DateEnd;
                    dialog.AppointmentTitle = e.Appointment.Subject;
                    dialog.LichId = e.Appointment.LichId;
                    dialog.LichChiTietId = e.Appointment.LichChiTietId;
                    dialog.NgayTrongTuanId = e.Appointment.NgayTrongTuanId;

                    DialogResult result = dialog.ShowDialog();
                    if (result == DialogResult.OK)// Cap nhat lich san cau long
                    {
                        //if the user clicked 'save', update the appointment dates and title
                        e.Appointment.DateStart = dialog.AppointmentDateStart;
                        e.Appointment.DateEnd = dialog.AppointmentDateEnd;
                        e.Appointment.Subject = dialog.AppointmentTitle;
                        e.Appointment.LichChiTietId = dialog.LichChiTietId;
                        e.Appointment.LichId = dialog.LichId;
                        e.Appointment.NgayTrongTuanId = dialog.NgayTrongTuanId;

                        clsLichChiTietDTO lichChiTietDTO = new clsLichChiTietDTO();
                        lichChiTietDTO.LichChiTietId = dialog.LichChiTietId;
                        lichChiTietDTO.LichId = dialog.LichId;
                        lichChiTietDTO.NgayTrongTuanId = dialog.NgayTrongTuanId;
                        lichChiTietDTO.GioBatDau = dialog.AppointmentDateStart.ToShortTimeString();
                        lichChiTietDTO.GioKetThuc = dialog.AppointmentDateEnd.ToShortTimeString();

                        clsLichChiTietBUS LCTBUS = new clsLichChiTietBUS();
                        int kq = LCTBUS.Sua(lichChiTietDTO);
                        if (kq == -1)
                        {
                            MessageBox.Show("Lỗi cập nhật lịch chi tiết sân cầu lông", "Thông báo");
                            return;
                        }
                        MessageBox.Show("Cập nhật lịch chi tiết sân cầu thành công", "Thông báo");
                        OnLoad(e);
                        return;
                    }
                    // Xoa lich tren giao dien
                    else if (result == DialogResult.Abort)
                    {
                        int IDLichChiTiet = dialog.LichChiTietId;
                        int kq = LichChiTietBUS.Xoa(IDLichChiTiet);
                        if (kq == -1)
                        {
                            MessageBox.Show("Lỗi xóa lịch chi tiết sân cầu lông", "Thông báo");
                            return;
                        }
                        MessageBox.Show("Xóa lịch chi tiết sân cầu thành công", "Thông báo");
                        OnLoad(e);
                        return;
                    }
                }
                #endregion
            }
            catch { }
        }
        #endregion

        #region calendar_AppointmentMove(object sender, AppointmentEditEventArgs e)
        private void calendar_AppointmentMove(object sender, AppointmentMoveEventArgs e)
        {
            try
            {
                //show a dialog to move the appointment date
                using (frmChuyenLichSanCauLong dialog = new frmChuyenLichSanCauLong())
                {
                    dialog.AppointmentOldDateStart = e.Appointment.DateStart;
                    dialog.AppointmentOldDateEnd = e.Appointment.DateEnd;
                    dialog.AppointmentTitle = e.Appointment.Subject;
                    dialog.LichId = e.Appointment.LichId;
                    dialog.LichChiTietId = e.Appointment.LichChiTietId;
                    dialog.NgayTrongTuanId = e.Appointment.NgayTrongTuanId;
                    if (e.NewDate != null)
                    {
                        switch (e.NewDate.DayOfWeek.ToString().ToUpper())
                        {
                            case "MONDAY":
                                dialog.NgayTrongTuanId = 2;
                                break;
                            case "TUESDAY":
                                dialog.NgayTrongTuanId = 3;
                                break;
                            case "WEDNESDAY":
                                dialog.NgayTrongTuanId = 4;
                                break;
                            case "THURSDAY":
                                dialog.NgayTrongTuanId = 5;
                                break;
                            case "FRIDAY":
                                dialog.NgayTrongTuanId = 6;
                                break;
                            case "SATURDAY":
                                dialog.NgayTrongTuanId = 7;
                                break;
                            case "SUNDAY":
                                dialog.NgayTrongTuanId = 8;
                                break;
                        }
                        dialog.AppointmentDateStart = e.NewDate;
                        dialog.AppointmentDateEnd = new DateTime(e.NewDate.Ticks + (dialog.AppointmentOldDateEnd.Ticks - dialog.AppointmentOldDateStart.Ticks));
                    }
                    DialogResult result = dialog.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        //if the user clicked 'save', update the appointment dates
                        //e.Appointment.DateStart = dialog.AppointmentDateStart;
                        //e.Appointment.DateEnd = dialog.AppointmentDateEnd;

                        //have to tell the controls to refresh appointment display
                        //dscSanCau01.RefreshAppointments();

                        //get the controls to repaint 
                        //dscSanCau01.Invalidate();
                        clsLichChiTietDTO lichChiTietDTO = new clsLichChiTietDTO();
                        lichChiTietDTO.LichChiTietId = dialog.LichChiTietId;
                        lichChiTietDTO.LichId = dialog.LichId;
                        lichChiTietDTO.NgayTrongTuanId = dialog.NgayTrongTuanId;
                        lichChiTietDTO.GioBatDau = dialog.AppointmentDateStart.ToShortTimeString();
                        lichChiTietDTO.GioKetThuc = dialog.AppointmentDateEnd.ToShortTimeString();

                        clsLichChiTietBUS LCTBUS = new clsLichChiTietBUS();
                        int kq = LCTBUS.Sua(lichChiTietDTO);
                        if (kq == -1)
                        {
                            MessageBox.Show("Lỗi cập nhật lịch chi tiết sân cầu lông", "Thông báo");
                            return;
                        }
                        MessageBox.Show("Cập nhật lịch chi tiết sân cầu thành công", "Thông báo");
                        OnLoad(e);
                        return;
                    }
                    else if (result == DialogResult.Yes)
                    {
                        // sao chep lich san
                        string title = dialog.AppointmentTitle;
                        DateTime dateStart = dialog.AppointmentDateStart;
                        DateTime dateEnd = dialog.AppointmentDateEnd;
                        e.Control.Appointments.Add(new ExtendedAppointment() { Subject = title, DateStart = dateStart, DateEnd = dateEnd });

                        //have to tell the controls to refresh appointment display
                        dscSanCau01.RefreshAppointments();

                        //get the controls to repaint 
                        dscSanCau01.Invalidate();
                    }
                }
            }
            catch { }    
        }
        #endregion

        #region calendar_AppointmentAdd(object sender, AppointmentCreateEventArgs e)
        private void calendar_AppointmentAdd(object sender, AppointmentCreateEventArgs e)
        {
            //show a dialog to add an appointment
            try
            {
                using (frmThemLichSanCauLong dialog = new frmThemLichSanCauLong())
                {
                    if (e.Date != null)
                    {
                        dialog.AppointmentDateStart = e.Date.Value;
                        dialog.AppointmentDateEnd = e.Date.Value.AddHours(2);
                    }
                    DialogResult result = dialog.ShowDialog();
                    if (result == DialogResult.Cancel)
                    {
                        return;
                    }
                    
                    //Thu nghiem ngay 28/10/13

                    #region Xac dinh san cau
                    int SanCauId = 0;
                    DataTable dtSanCau = SanCauBUS.LayBang();
                    if (dtSanCau.Rows.Count == 2)
                    {

                        switch (tabSanCau.SelectedTab.Name.ToString())
                        {
                            case "tbSan01":
                                SanCauId = int.Parse(dtSanCau.Rows[0]["SanCauId"].ToString());
                                break;
                            case "tbSan02":
                                SanCauId = int.Parse(dtSanCau.Rows[1]["SanCauId"].ToString());
                                break;
                            case "tbSan03":
                                //MessageBox.Show("Sân cầu chưa có trong câu lạc bộ", "Thông báo");
                                break;
                        }
                    }
                    else if (dtSanCau.Rows.Count == 3)
                    {
                        switch (tabSanCau.SelectedTab.Name.ToString())
                        {
                            case "tbSan01":
                                SanCauId = int.Parse(dtSanCau.Rows[0]["SanCauId"].ToString());
                                break;
                            case "tbSan02":
                                SanCauId = int.Parse(dtSanCau.Rows[1]["SanCauId"].ToString());
                                break;
                            case "tbSan03":
                                SanCauId = int.Parse(dtSanCau.Rows[2]["SanCauId"].ToString());
                                break;
                        }
                    }
                    #endregion
                    
                    
                    string title = dialog.AppointmentTitle;
                    DateTime dateStart = dialog.AppointmentDateStart;
                    DateTime dateEnd = dialog.AppointmentDateEnd;
                    
                    #region Kiem tra lich co bi trung hay khong
                    clsLichChiTietBUS LichChiTietBUS = new clsLichChiTietBUS();
                    DataTable dtBangLichChiTietTheoSanCauID = LichChiTietBUS.LayBangTheoSanCauId(SanCauId);
                    DataView dvBangLichChiTietTheoSanCauId = new DataView(dtBangLichChiTietTheoSanCauID);
                    int NgayDangKy = 0;
                    switch (dateStart.Date.DayOfWeek.ToString().ToUpper())
                    {
                        case "MONDAY":
                            NgayDangKy = 2;
                            break;
                        case "TUESDAY":
                            NgayDangKy = 3;
                            break;
                        case "WEDNESDAY":
                            NgayDangKy = 4;
                            break;
                        case "THURSDAY":
                            NgayDangKy = 5;
                            break;
                        case "FRIDAY":
                            NgayDangKy = 6;
                            break;
                        case "SATURDAY":
                            NgayDangKy = 7;
                            break;
                        case "SUNDAY":
                            NgayDangKy = 8;
                            break;
                    }
                    dvBangLichChiTietTheoSanCauId.RowFilter = "NgayTrongTuanId = " + NgayDangKy.ToString();
                    DataTable dtTemp1 = dvBangLichChiTietTheoSanCauId.ToTable();
                    foreach (DataRow dtrow in dtTemp1.Rows)
                    {
                        int GioKetThucDangXet = dateEnd.Hour;
                        int GioBatDau = int.Parse(dtrow["GioBatDau"].ToString().Split(':')[0]);
                        int GioKetThuc = int.Parse(dtrow["GioKetThuc"].ToString().Split(':')[0]);
                        if (GioBatDau < GioKetThucDangXet && GioKetThucDangXet <= GioKetThuc)
                        {
                            MessageBox.Show("Lịch đăng ký bị trùng với lịch thuê sân của khách hàng <<" + dtrow["HoTen"].ToString() + ">>.\nVui lòng kiểm tra lại thời gian."
                                , "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    } 
                    #endregion

                    #region Ghi du lieu vao bang LICH
                    clsLichDTO lichDTO = new clsLichDTO();
                    lichDTO.KhachHangId = dialog.KhachHangId;
                    int IDKhachHang = dialog.KhachHangId;
                    lichDTO.SanCauId = SanCauId;
                    if (rbtKhongThoiHan.Checked == true)
                    {
                        lichDTO.NgayBatDau = DateTime.Now;
                        lichDTO.NgayKetThuc = DateTime.Now;
                    }
                    else if (rbtCoThoiHan.Checked == true)
                    {
                        lichDTO.NgayBatDau = dtpNgayBatDau.Value;
                        lichDTO.NgayKetThuc = dtpNgayKetThuc.Value;
                    }

                    int LichId = -1;
                    clsLichBUS lichBUS = new clsLichBUS();
                    DataTable dt = lichBUS.KiemTraTonTaiKhachHangVaSanCauTrongLich(lichDTO);
                    if (dt.Rows.Count > 0)
                    {
                        LichId = int.Parse(dt.Rows[0]["LichId"].ToString());
                    }
                    else
                        LichId = lichBUS.Them(lichDTO);
                    if (LichId == -1)
                    {
                        MessageBox.Show("Lỗi thêm lịch sân cầu lông", "Thông báo");
                        return;
                    }
                    #endregion

                    #region Ghi du lieu vao bang LICH CHI TIET
                    clsLichChiTietDTO lichChiTietDTO = new clsLichChiTietDTO();
                    int GioDoiGiaTienSan = 15;
                    lichChiTietDTO.LichId = LichId;
                    switch (dateStart.Date.DayOfWeek.ToString().ToUpper())
                    {
                        case "MONDAY":
                            lichChiTietDTO.NgayTrongTuanId = 2;
                            break;
                        case "TUESDAY":
                            lichChiTietDTO.NgayTrongTuanId = 3;
                            break;
                        case "WEDNESDAY":
                            lichChiTietDTO.NgayTrongTuanId = 4;
                            break;
                        case "THURSDAY":
                            lichChiTietDTO.NgayTrongTuanId = 5;
                            break;
                        case "FRIDAY":
                            lichChiTietDTO.NgayTrongTuanId = 6;
                            break;
                        case "SATURDAY":
                            lichChiTietDTO.NgayTrongTuanId = 7;
                            break;
                        case "SUNDAY":
                            lichChiTietDTO.NgayTrongTuanId = 8;
                            break;
                    }
                    if (dateStart.Hour < GioDoiGiaTienSan && GioDoiGiaTienSan < dateEnd.Hour && lichChiTietDTO.NgayTrongTuanId < 7)
                    // GBD < 15h < GKT va Ngay trong tuan
                    {
                        //clsLichChiTietBUS LichChiTietBUS = new clsLichChiTietBUS();
                        lichChiTietDTO.GioBatDau = dateStart.ToShortTimeString();
                        lichChiTietDTO.GioKetThuc = GioDoiGiaTienSan.ToString() + ":00";
                        lichChiTietDTO.MoTa = dialog.MoTa;
                        int kq = LichChiTietBUS.Them(lichChiTietDTO);
                        if (kq == -1)
                        {
                            MessageBox.Show("Lỗi thêm lịch chi tiết sân cầu lông", "Thông báo");
                            return;
                        }
                        lichChiTietDTO.GioBatDau = GioDoiGiaTienSan.ToString() + ":00";
                        lichChiTietDTO.GioKetThuc = dateEnd.ToShortTimeString();
                        kq = LichChiTietBUS.Them(lichChiTietDTO);
                        if (kq == -1)
                        {
                            MessageBox.Show("Lỗi thêm lịch chi tiết sân cầu lông", "Thông báo");
                            return;
                        }
                        MessageBox.Show("Thêm lịch sân cầu thành công", "Thông báo");
                        OnLoad(e);
                    }
                    else
                    {
                        lichChiTietDTO.GioBatDau = dateStart.ToShortTimeString();
                        lichChiTietDTO.GioKetThuc = dateEnd.ToShortTimeString();
                        lichChiTietDTO.MoTa = dialog.MoTa;
                        //clsLichChiTietBUS LichChiTietBUS = new clsLichChiTietBUS();
                        int kq = LichChiTietBUS.Them(lichChiTietDTO);
                        if (kq == -1)
                        {
                            MessageBox.Show("Lỗi thêm lịch chi tiết sân cầu lông", "Thông báo");
                            return;
                        }
                        MessageBox.Show("Thêm lịch sân cầu thành công", "Thông báo");
                        OnLoad(e);
                    }
                    #endregion
                    
                    if (result == DialogResult.Retry)
                    {
                        // khong tinh toan ghi vao cong no, tiep tuc cho nguoi su dung dat lich.
                    }
                    // KET THUC cua phan Thu nghiem ngay 28/10/13

                    else if (result == DialogResult.OK) // Ghi lich va tinh toan ghi vao cong no
                    {
                        //if the user clicked 'save', save the new appointment 
                        #region tam dong lai NGAY 28/10/2013
                        //int SanCauId = 0;
                        //DataTable dtSanCau = SanCauBUS.LayBang();
                        //if (dtSanCau.Rows.Count == 2)
                        //{

                        //    switch (tabSanCau.SelectedTab.Name.ToString())
                        //    {
                        //        case "tbSan01":
                        //            SanCauId = int.Parse(dtSanCau.Rows[0]["SanCauId"].ToString());
                        //            break;
                        //        case "tbSan02":
                        //            SanCauId = int.Parse(dtSanCau.Rows[1]["SanCauId"].ToString());
                        //            break;
                        //        case "tbSan03":
                        //            //MessageBox.Show("Sân cầu chưa có trong câu lạc bộ", "Thông báo");
                        //            break;
                        //    }
                        //}
                        //else if (dtSanCau.Rows.Count == 3)
                        //{
                        //    switch (tabSanCau.SelectedTab.Name.ToString())
                        //    {
                        //        case "tbSan01":
                        //            SanCauId = int.Parse(dtSanCau.Rows[0]["SanCauId"].ToString());
                        //            break;
                        //        case "tbSan02":
                        //            SanCauId = int.Parse(dtSanCau.Rows[1]["SanCauId"].ToString());
                        //            break;
                        //        case "tbSan03":
                        //            SanCauId = int.Parse(dtSanCau.Rows[2]["SanCauId"].ToString());
                        //            break;
                        //    }
                        //}
                        //string title = dialog.AppointmentTitle;
                        //DateTime dateStart = dialog.AppointmentDateStart;
                        //DateTime dateEnd = dialog.AppointmentDateEnd;
                        
                        //#region Ghi du lieu vao bang LICH
                        //clsLichDTO lichDTO = new clsLichDTO();
                        //lichDTO.KhachHangId = dialog.KhachHangId;
                        //int IDKhachHang = dialog.KhachHangId;
                        //lichDTO.SanCauId = SanCauId;
                        //if (rbtKhongThoiHan.Checked == true)
                        //{
                        //    lichDTO.NgayBatDau = DateTime.Now;
                        //    lichDTO.NgayKetThuc = DateTime.Now;
                        //}
                        //else if (rbtCoThoiHan.Checked == true)
                        //{
                        //    lichDTO.NgayBatDau = dtpNgayBatDau.Value;
                        //    lichDTO.NgayKetThuc = dtpNgayKetThuc.Value;
                        //}

                        //int LichId = -1;
                        //clsLichBUS lichBUS = new clsLichBUS();
                        //DataTable dt = lichBUS.KiemTraTonTaiKhachHangVaSanCauTrongLich(lichDTO);
                        //if (dt.Rows.Count > 0)
                        //{
                        //    LichId = int.Parse(dt.Rows[0]["LichId"].ToString());
                        //}
                        //else
                        //    LichId = lichBUS.Them(lichDTO);
                        //if (LichId == -1)
                        //{
                        //    MessageBox.Show("Lỗi thêm lịch sân cầu lông", "Thông báo");
                        //    return;
                        //}
                        //#endregion

                        //#region Ghi du lieu vao bang LICH CHI TIET
                        //clsLichChiTietDTO lichChiTietDTO = new clsLichChiTietDTO();
                        //int GioDoiGiaTienSan = 15;
                        //lichChiTietDTO.LichId = LichId;
                        //switch (dateStart.Date.DayOfWeek.ToString().ToUpper())
                        //{
                        //    case "MONDAY":
                        //        lichChiTietDTO.NgayTrongTuanId = 2;
                        //        break;
                        //    case "TUESDAY":
                        //        lichChiTietDTO.NgayTrongTuanId = 3;
                        //        break;
                        //    case "WEDNESDAY":
                        //        lichChiTietDTO.NgayTrongTuanId = 4;
                        //        break;
                        //    case "THURSDAY":
                        //        lichChiTietDTO.NgayTrongTuanId = 5;
                        //        break;
                        //    case "FRIDAY":
                        //        lichChiTietDTO.NgayTrongTuanId = 6;
                        //        break;
                        //    case "SATURDAY":
                        //        lichChiTietDTO.NgayTrongTuanId = 7;
                        //        break;
                        //    case "SUNDAY":
                        //        lichChiTietDTO.NgayTrongTuanId = 8;
                        //        break;
                        //}
                        //if (dateStart.Hour < GioDoiGiaTienSan && GioDoiGiaTienSan < dateEnd.Hour && lichChiTietDTO.NgayTrongTuanId < 7)
                        //// GBD < 15h < GKT va Ngay trong tuan
                        //{
                        //    clsLichChiTietBUS LCTBUS = new clsLichChiTietBUS();
                        //    lichChiTietDTO.GioBatDau = dateStart.ToShortTimeString();
                        //    lichChiTietDTO.GioKetThuc = GioDoiGiaTienSan.ToString() + ":00";
                        //    int kq = LCTBUS.Them(lichChiTietDTO);
                        //    if (kq == -1)
                        //    {
                        //        MessageBox.Show("Lỗi thêm lịch chi tiết sân cầu lông", "Thông báo");
                        //        return;
                        //    }
                        //    lichChiTietDTO.GioBatDau = GioDoiGiaTienSan.ToString() + ":00";
                        //    lichChiTietDTO.GioKetThuc = dateEnd.ToShortTimeString();
                        //    kq = LCTBUS.Them(lichChiTietDTO);
                        //    if (kq == -1)
                        //    {
                        //        MessageBox.Show("Lỗi thêm lịch chi tiết sân cầu lông", "Thông báo");
                        //        return;
                        //    }
                        //    MessageBox.Show("Thêm lịch sân cầu thành công", "Thông báo");
                        //    OnLoad(e);
                        //}
                        //else
                        //{
                        //    lichChiTietDTO.GioBatDau = dateStart.ToShortTimeString();
                        //    lichChiTietDTO.GioKetThuc = dateEnd.ToShortTimeString();
                        //    clsLichChiTietBUS LCTBUS = new clsLichChiTietBUS();
                        //    int kq = LCTBUS.Them(lichChiTietDTO);
                        //    if (kq == -1)
                        //    {
                        //        MessageBox.Show("Lỗi thêm lịch chi tiết sân cầu lông", "Thông báo");
                        //        return;
                        //    }
                        //    MessageBox.Show("Thêm lịch sân cầu thành công", "Thông báo");
                        //    OnLoad(e);
                        //}
                        //#endregion
                        #endregion 

                        #region Tinh toan dua vao bang CONG NO
                        if (title.Contains("KH vãng lai"))
                        {
                            return;
                        }
                        int CurrentMonth = DateTime.Now.Month;
                        int CurrentDay = DateTime.Now.Day;
                        int CurrentYear = DateTime.Now.Year;

                        //clsLichChiTietBUS LichChiTietBUS = new clsLichChiTietBUS();
                        DataTable dtTemp = LichChiTietBUS.LayBangThongBaoTienSanHangThang();
                        #region xac dinh ngay cuoi thang
                        int EndDayOfMonth = 0;
                        switch (CurrentMonth)
                        {
                            case 1:
                                EndDayOfMonth = 31;
                                break;
                            case 2:
                                EndDayOfMonth = 28;
                                break;
                            case 3:
                                EndDayOfMonth = 31;
                                break;
                            case 4:
                                EndDayOfMonth = 30;
                                break;
                            case 5:
                                EndDayOfMonth = 31;
                                break;
                            case 6:
                                EndDayOfMonth = 30;
                                break;
                            case 7:
                                EndDayOfMonth = 31;
                                break;
                            case 8:
                                EndDayOfMonth = 31;
                                break;
                            case 9:
                                EndDayOfMonth = 30;
                                break;
                            case 10:
                                EndDayOfMonth = 31;
                                break;
                            case 11:
                                EndDayOfMonth = 30;
                                break;
                            case 12:
                                EndDayOfMonth = 31;
                                break;
                        }
                        #endregion

                        double TongTienTheoKhachHangId = 0;
                        DataView dv = new DataView(dtTemp);
                        dv.RowFilter = "KhachHangId = " + IDKhachHang.ToString();
                        DataTable KhachHangTienSanTable = dv.ToTable(true, "KhachHangId", "MaKhachHang", "HoTen");
                        KhachHangTienSanTable.Columns.Add("SoTien", typeof(double));

                        DateTime dtNgayDauThang = new DateTime(CurrentYear, CurrentMonth, CurrentDay);
                        DateTime dtNgayCuoiThang = new DateTime(CurrentYear, CurrentMonth, EndDayOfMonth);
                        foreach (DataRow dr in KhachHangTienSanTable.Rows)
                        {
                            dv.RowFilter = "KhachHangId=" + dr["KhachHangId"].ToString();
                            DataTable tableFromView = dv.ToTable();
                            for (int i = 0; i < tableFromView.Rows.Count; i++)
                            {
                                int countDay = 0;
                                #region Dem Ngay
                                switch (tableFromView.Rows[i]["TenNgayTrongTuan"].ToString().ToLower())
                                {
                                    case "thứ hai":
                                        countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Monday, dtNgayDauThang, dtNgayCuoiThang);
                                        break;
                                    case "thứ ba":
                                        countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Tuesday, dtNgayDauThang, dtNgayCuoiThang);
                                        break;
                                    case "thứ tư":
                                        countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Wednesday, dtNgayDauThang, dtNgayCuoiThang);
                                        break;
                                    case "thứ năm":
                                        countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Thursday, dtNgayDauThang, dtNgayCuoiThang);
                                        break;
                                    case "thứ sáu":
                                        countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Friday, dtNgayDauThang, dtNgayCuoiThang);
                                        break;
                                    case "thứ bảy":
                                        countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Saturday, dtNgayDauThang, dtNgayCuoiThang);
                                        break;
                                    case "chủ nhật":
                                        countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Sunday, dtNgayDauThang, dtNgayCuoiThang);
                                        break;
                                }
                                #endregion
                                string[] BatDau = tableFromView.Rows[i]["GioBatDau"].ToString().Split(':');
                                string[] KetThuc = tableFromView.Rows[i]["GioKetThuc"].ToString().Split(':');
                                DateTime dtBatDau = new DateTime(1, 1, 1, int.Parse(BatDau[0]), int.Parse(BatDau[1]), int.Parse(BatDau[2]));
                                DateTime dtKetThuc = new DateTime(1, 1, 1, int.Parse(KetThuc[0]), int.Parse(KetThuc[1]), int.Parse(KetThuc[2]));
                                TimeSpan ts = dtKetThuc.Subtract(dtBatDau);
                                double TongTien = ts.TotalHours * double.Parse(tableFromView.Rows[i]["GiaTien"].ToString()) * countDay;
                                TongTienTheoKhachHangId += TongTien;
                            }
                            dr["SoTien"] = TongTienTheoKhachHangId;
                            TongTienTheoKhachHangId = 0;
                            dv.RowFilter = string.Empty;
                        }
                        clsCongNoCauLongBUS CongNoBUS = new clsCongNoCauLongBUS();
                        string strHoTen = string.Empty;
                        foreach (DataRow dr in KhachHangTienSanTable.Rows)
                        {
                            clsCongNoCauLongDTO CongNoDTO = new clsCongNoCauLongDTO();
                            CongNoDTO.KhachHangId = int.Parse(dr["KhachHangId"].ToString());
                            CongNoDTO.HoTen = dr["HoTen"].ToString();
                            strHoTen = dr["HoTen"].ToString();
                            CongNoDTO.SoTien = double.Parse(dr["SoTien"].ToString());
                            CongNoDTO.Thang = CurrentMonth;
                            CongNoDTO.Nam = CurrentYear;
                            CongNoDTO.TrangThaiGachNo = 0;
                            if (CongNoBUS.Them(CongNoDTO) == -1)
                            {
                                MessageBox.Show("Lỗi thêm dữ liệu công nợ <<MaKH " + dr["MaKhachHang"].ToString() + ">>", "Thông báo");
                            }
                        }
                        MessageBox.Show("Thêm dữ liệu công nợ tháng " + CurrentMonth.ToString() + "/" + CurrentYear.ToString() 
                            + " của khách hàng " + strHoTen + " thành công."
                            , "Thông báo");
                        #endregion
                    }// end of IF dialog result = OK
                }// end of show dialog
            }
            catch { }
        }
        #endregion

        #region btnLuuLichSan_Click(object sender, EventArgs e)
        private void btnLuuLichSan_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Xác nhận sao lưu lịch sân cầu lông?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Cancel)
                    return;
                DateTime temp = DateTime.Now;
                DateTime NgaySaoLuu = new DateTime(temp.Year, temp.Month, temp.Day);
                clsLichChiTietSaoLuuBUS LichChiTietSaoLuuBUS = new clsLichChiTietSaoLuuBUS();
                int kq = LichChiTietSaoLuuBUS.InsertLich(NgaySaoLuu);
                if (kq == 1)
                {
                    int kq1 = LichChiTietSaoLuuBUS.InsertLichChiTiet(NgaySaoLuu);
                    if (kq1 == 1)
                        MessageBox.Show("Sao lưu lịch sân cầu thành công.", "Thông báo");
                    else
                        MessageBox.Show("Lỗi sao lưu chi tiết lịch sân cầu.", "Thông báo");
                }
                else
                    MessageBox.Show("Lỗi sao lưu lịch sân cầu.", "Thông báo");
            }
            catch { }
        }
        #endregion

        #region tabSanCau_SelectedIndexChanged(object sender, EventArgs e)
        private void tabSanCau_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SoLuongSanCau == 2)
            {
                if (tabSanCau.SelectedTab.Name.ToString() == "tbSan03")
                    MessageBox.Show("Sân cầu chưa có trong câu lạc bộ. Vui lòng chọn sân cầu khác để xếp lịch!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region rbtCoThoiHan_CheckedChanged(object sender, EventArgs e)
        private void rbtCoThoiHan_CheckedChanged(object sender, EventArgs e)
        {
            lbNgayBD.Enabled = lbNgayKT.Enabled = rbtCoThoiHan.Checked;
            dtpNgayBatDau.Enabled = dtpNgayKetThuc.Enabled = rbtCoThoiHan.Checked;
        }
        #endregion

        private void frmDatLichSanCauMoi_Load(object sender, EventArgs e)
        {
            try
            {
                SetPermission();
            }
            catch { }
        }

        #endregion

    }
}
